// 画流程图
function drawProcess(processId, parentProcessId){
	// $("body").attr("onload", "initialize()");
	$("#svgPaint").empty();
	var position = getProcessPosition(processId);
	if (position.success) {
		// 添加当前流程信息
		setProcessInfo(processId, parentProcessId);
		var data = position.data;
		var width = data.width + 300;
		var height = data.height;
		var elements = data.elements;
		var viewBox = "0 0 " + width + " " + height;
		setViewBox(viewBox);
		var svg = d3.select("#svgPaint")
			.append("svg")
			.attr("id","svgProcess")
			.attr("width",width)
			.attr("height",height)
			.attr("viewBox",viewBox)
			// .attr("onload","Init(event)")
			// .attr("onmousedown","Grab()")
			// .attr("onmousemove","Drag()")
			// .attr("onmouseup","Drop()")
			.each(function (d) {
				if (parentProcessId > 0) {
					d3.select(this)
						.attr("onclick","processBack(" + parentProcessId + ")");
				}
			})
		svg.append("title").html("流程图");
		var defs = svg.append("defs");
		// 箭头
		drawMarkerArrow(defs, defaultColor);
		drawMarkerArrow(defs, passedColor);
		// 垂直渐变
		drawLinearGradient(defs, "#ffffff", "#ffffcc");
		var pathStatus;
		var processEvent;
		var pathData = getProcessPath(processId);
		var subProcessData = getSubProcess(processId);
		$.each(elements,function(i, element) {	
			pathStatus = getProcessStatus(element.id, pathData);
			processEvent = getProcessEvent(element.id, subProcessData, processId);
			drawPicture(svg, element, pathStatus, processEvent);
		});
	}
};

// 画图
function drawPicture(svg, element, pathStatus, processEvent) {
	var elementCoordinates = element.elementCoordinate;
	var elementType = element.type;
	var elementName = element.name;
	var elementId = element.id;
	// dasharray画虚线 dasharray="2 2"
	var dasharray = "";
	var size = 1;
	var strokeWidth;
	var strokeColor = pathStatus.strokeColor;
	var pathTitle = pathStatus.pathTitle;
	//直线流程线
	if (elementType == "flow") {
		var positions = [];
		$.each(elementCoordinates, function(i, m) {
			var position = {};
			position.x = m.x;
			position.y = m.y;
			positions[i] = position;
		});
		drawFlow(svg, elementId, strokeColor, positions);
	} else {
		var x = elementCoordinates.x;
		var y = elementCoordinates.y;
		var width = elementCoordinates.width;
		var height = elementCoordinates.height;
		var cr = width/2;
		var cx = x + cr;
		var cy = y + cr;
		// 开始结束
		if (elementType == "startEvent" || elementType == "endEvent") {
			if (elementType == "startEvent") {
				strokeWidth = 1;
			} else if (elementType == "endEvent") {
				strokeWidth = 3;
			}
			drawCircle(svg, elementId, strokeColor, "white", cx, cy, cr, strokeWidth, "", pathTitle);
		}
		//用户任务 && 子流程
		if (elementType == "userTask" || elementType == "serviceTask" || elementType == "receiveTask" || elementType == "scriptTask" || elementType == "callActivity" || elementType == "subProcess") {
			// rx,ry设置矩形的四个角弧度
			var rx = 5;
			var ry = 5;
			var fillcolor = "url(#change)";
			var fontWeight = "normal";
			if (elementType == "subProcess") {
				rx = 0;
				ry = 0;
				fillcolor = "white";
			}
			strokeWidth = 1;
			if (elementType == "callActivity") {
				strokeWidth = 3;
				fontWeight = "bolder";
			}
			drawRect(svg, elementId, strokeColor, x, y, width, height, rx, ry, fillcolor, strokeWidth, processEvent, pathTitle);
			if (elementName != "") {
				if (elementType != "subProcess") {
					drawFont(svg, elementId, strokeColor, x + width/2, y + height*1/4, elementName, 12, fontWeight, 8, pathTitle);
				} else {
					drawFont(svg, elementId, strokeColor, x + width/2, y, elementName, 12, fontWeight, 60, pathTitle);
				};
			}
			if (elementType == "userTask") {
				drawUserTask(svg, elementId, x, y, width, height, strokeColor, pathTitle);
			} else if (elementType == "serviceTask") {
				drawServiceTask(svg, elementId, x, y, width, height, strokeColor, pathTitle);
			} else if (elementType == "receiveTask") {
				drawReceiveTask(svg, elementId, x, y, width, height, strokeColor, pathTitle);
			} else if (elementType == "scriptTask") {
				drawScriptTask(svg, elementId, x, y, width, height, strokeColor, pathTitle);
			} else if (elementType == "callActivity") {
				// drawCallActivity(svg, elementId, x, y, width, height, size);
			} else if (elementType == "subProcess") {
				// drawSubProcess(svg, elementId, x, y, width, height, size);
			}
		}
		//网关
		if (elementType == "exclusiveGateway" || elementType == "inclusiveGateway" || elementType == "parallelGateway") {
			drawGateway(svg, elementId, strokeColor, x, y, width, height, elementType, pathTitle)
			// // 先画一个菱形
			// drawDiamond(svg, elementId, strokeColor, cx, cy, width, height, pathTitle);
			// // 再根据类型画其他图形
			// strokeWidth = 4;
			// var points = "";
			// if (elementType == "exclusiveGateway") {
			// 	//单一分支
			// 	var xp = width/5;
			// 	var yp = height/5;
			// 	points = (cx-xp) + "," + (cy-yp) +" " + (cx+xp) + "," + (cy+yp);
			// 	drawPolyLine(svg, elementId, strokeColor, points, strokeWidth);
			// 	points = "";
			// 	points = (cx+xp) + "," + (cy-yp) +" " + (cx-xp) + "," + (cy+yp);
			// 	drawPolyLine(svg, elementId, strokeColor, points, strokeWidth);
			// } else if (elementType == "inclusiveGateway") {
			// 	//包含分支
			// 	drawCircle(svg, elementId, strokeColor, cx, cy, width/4, 3, "");
			// } else if (elementType == "parallelGateway") {
			// 	//并行分支
			// 	var xc = width/4;
			// 	var yc = height/4;
			// 	points = "";
			// 	points = (cx-xc) + "," + cy +" " + (cx+xc) + "," + cy;
			// 	drawPolyLine(svg, elementId, strokeColor, points, strokeWidth);
			// 	points = "";
			// 	points = cx + "," + (cy-yc) +" " + cx + "," + (cy+yc);
			// 	drawPolyLine(svg, elementId, strokeColor, points, strokeWidth);
			// };
		}
		//计时器
		if (elementType == "boundaryTimer" || elementType == "intermediateTimer") {
			drawTimer(svg, elementId, strokeColor, x, y, width, height, size, dasharray, pathTitle);
		}
		//信息边界事件
		if (elementType == "boundarySignal") {
			drawSignal(svg, elementId, strokeColor, x, y, width, height, size, dasharray, pathTitle);
		}
		// 错误
		if (elementType == "boundaryError" || elementType == "errorEndEvent") {
			drawError(svg, elementId, strokeColor, x, y, width, height, size, dasharray, pathTitle);
		}
	}
};
